//
//  TuisuanViewController.m
//  LiuHeBao
//
//  Created by zr on 2018/8/22.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "TuisuanViewController.h"
#import "TuiSuanTopView.h"
#import "LovePairView.h"
#import "ZhougongView.h"
#import "YuanView.h"
#import "XinzuoZone.h"
#import "ChooseView.h"
#import "TuisuanInfoViewController.h"
#import "PGDatePickManager.h"

@interface TuisuanViewController ()<PGDatePickerDelegate>

@property (nonatomic, copy)NSString * type;
@property (nonatomic, copy)NSString * chooseType;
@property (nonatomic, strong)LovePairView * loveView;
@property (nonatomic, strong)YuanView * yuanView;
@property (nonatomic, strong)XinzuoZone * xinzuoView;
@property (nonatomic, strong)ZhougongView * zhouView;
@property (nonatomic, strong)ChooseView * chooseView;


@end

@implementation TuisuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    self.type = @"one";
    
    
    
    [self setupNSNoti];
    [self setupUI];
}

- (void)setupUI {
    
    TuiSuanTopView * topView = [[TuiSuanTopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 45 * SCREENH6)];
    
    [self.view addSubview:topView];
    
    self.loveView = [[LovePairView alloc] initWithFrame:CGRectMake(0, 45 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT- 64 - 45 * SCREENH6)];
    self.loveView.hidden = NO;
    [self.view addSubview:self.loveView];
    
    self.yuanView = [[YuanView alloc] initWithFrame:CGRectMake(0, 45 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT- 64 - 45 * SCREENH6)];
    self.yuanView.hidden = YES;
    [self.view addSubview:self.yuanView];
    
    self.xinzuoView = [[XinzuoZone alloc] initWithFrame:CGRectMake(0, 45 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT- 64 - 45 * SCREENH6)];
    self.xinzuoView.hidden = YES;
    [self.view addSubview:self.xinzuoView];
    
    self.zhouView = [[ZhougongView alloc] initWithFrame:CGRectMake(0, 45 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT- 64 - 45 * SCREENH6)];
    self.zhouView.hidden = YES;
    [self.view addSubview:self.zhouView];
    
    
}


- (void)setupNSNoti{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTuiSuanData:) name:@"changeTuiSuanData" object:nil];
    
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(clickChooseButton:) name:@"clickChooseButton" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickSelectButton:) name:@"clickSelectButton" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToPairInfoVC:) name:@"pushToPairInfoVC" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickYuanChoose:) name:@"clickYuanChoose" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickXinzuoChoose:) name:@"clickXinzuoChoose" object:nil];
}


- (void)clickXinzuoChoose:(NSNotification *)noti{
    
    NSString * type = noti.userInfo[@"type"];
    
    if ([type isEqualToString:@"xinzuo"]) {
        
        self.chooseView = [[ChooseView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.chooseView.chooseType = type;
        
        [self.tabBarController.view addSubview:self.chooseView];
    }else{
        
        [self presentDateController];
    }
}


- (void)clickYuanChoose:(NSNotification *)noti{
    
    NSString * type = noti.userInfo[@"type"];
    
    if ([type isEqualToString:@"sheng"]) {
        
        self.chooseView = [[ChooseView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.chooseView.chooseType = type;
        
        [self.tabBarController.view addSubview:self.chooseView];
    }else{
        
        [self presentDateController];
    }
}


- (void)clickChooseButton:(NSNotification *)noti{
    
    self.chooseType = noti.userInfo[@"type"];
    
    self.chooseView = [[ChooseView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    if ([self.chooseType isEqualToString:@"xinzuoBoy"]||[self.chooseType isEqualToString:@"xinzuoGril"]){
        
        self.chooseView.chooseType = @"xinzuo";
    }else if ([self.chooseType isEqualToString:@"shengBoy"] || [self.chooseType isEqualToString:@"shengGirl"]){
        
        self.chooseView.chooseType = @"sheng";
    }else if ([self.chooseType isEqualToString:@"styleBoy"] || [self.chooseType isEqualToString:@"styleGirl"]){
        
        self.chooseView.chooseType = @"style";
    }
    
    
    
    [self.tabBarController.view addSubview:self.chooseView];
}


- (void)clickSelectButton:(NSNotification *)noti{
    
    [self.chooseView removeFromSuperview];
    
    NSString * type = noti.userInfo[@"type"];
    
    if ([type isEqualToString:@"sure"]) {
        
        NSString * value = noti.userInfo[@"value"];
        
        if ([self.type isEqualToString:@"one"]) {
            
            if ([self.chooseType isEqualToString:@"xinzuoBoy"]) {
                
                self.loveView.xinzuoBoy = value;
            }else if ([self.chooseType isEqualToString:@"xinzuoGril"]){
                
                self.loveView.xinzuoGirl = value;
            }else if ([self.chooseType isEqualToString:@"shengBoy"]){
                
                self.loveView.shengBoy = value;
            }else if ([self.chooseType isEqualToString:@"shengGirl"]){
                
                self.loveView.shengGirl = value;
            }else if ([self.chooseType isEqualToString:@"styleBoy"]){
                
                self.loveView.styleBoy = value;
            }else if ([self.chooseType isEqualToString:@"styleGirl"]){
                
                self.loveView.styleGirl = value;
            }
        }else if ([self.type isEqualToString:@"two"]){
            
            self.yuanView.shengStr = value;
        }else if ([self.type isEqualToString:@"three"]){
            
            self.xinzuoView.shengStr = value;
        }
        
    }
}


- (void)pushToPairInfoVC:(NSNotification *)noti{
    
    SHOWHUD;
    
    NSDictionary * dict = noti.userInfo;
    NSString * str = noti.userInfo[@"type"];
    
    if ([str isEqualToString:@"3"]) {
        
        str= @"星座分析";
    }else if ([str isEqualToString:@"4"]){
        
        str = @"生肖分析";
    }else if ([str isEqualToString:@"2"]){
        
        str = @"血型分析";
    }
    
    
    [[NetworkTool sharedTools] POST:@"index/Liuhetreasure/pair" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        HIDDENHUD;
        int status = [responseObject[@"status"] intValue];
        
        if (status == 200) {
            
            NSDictionary * data = responseObject[@"data"];
            
            HIDDEN_TAB;
            
            TuisuanInfoViewController * infoVC = [TuisuanInfoViewController new];
            
            infoVC.titleStr = str;
            infoVC.content = data[@"content"];
            infoVC.ID = data[@"id"];
            
            [self.navigationController pushViewController:infoVC animated:YES];
            
            SHOW_TAB;
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        HIDDENHUD;
    }];
    
    
    
}



- (void)changeTuiSuanData:(NSNotification *)noti{
    
    self.type = noti.userInfo[@"type"];
    
    if ([self.type isEqualToString:@"one"]) {
        
        self.loveView.hidden = NO;
        
        self.yuanView.hidden = YES;
        self.xinzuoView.hidden = YES;
        self.zhouView.hidden = YES;
    }else if ([self.type isEqualToString:@"two"]){
        
        self.loveView.hidden = YES;
        
        self.yuanView.hidden = NO;
        self.xinzuoView.hidden = YES;
        self.zhouView.hidden = YES;
    }else if ([self.type isEqualToString:@"three"]){
        
        self.loveView.hidden = YES;
        
        self.yuanView.hidden = YES;
        self.xinzuoView.hidden = NO;
        self.zhouView.hidden = YES;
    }else if ([self.type isEqualToString:@"four"]){
        
        self.loveView.hidden = YES;
        
        self.yuanView.hidden = YES;
        self.xinzuoView.hidden = YES;
        self.zhouView.hidden = NO;
    }
}



- (void)presentDateController{
    
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    datePickManager.isShadeBackgroud = true;
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    datePicker.datePickerType = PGDatePickerType1;
    datePicker.isHiddenMiddleText = false;
    datePicker.datePickerMode = PGDatePickerModeDate;
    //设置确定按钮的字体颜色
    datePickManager.confirmButtonTextColor = MAINCOLOR;
    //设置选中行的字体颜色
    datePicker.textColorOfSelectedRow = MAINCOLOR;
    datePicker.language = @"zh-Hans";
    datePicker.middleTextColor = MAINCOLOR;
    
    [self presentViewController:datePickManager animated:false completion:nil];
}


#pragma PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    
    //    self.birthday = [NSString stringWithFormat:@"%li-%02li-%02li",(long)dateComponents.year,dateComponents.month,dateComponents.day];
    
    NSString * str = [NSString stringWithFormat:@"%ld年%ld月%li日",(long)dateComponents.year,(long)dateComponents.month,dateComponents.day];
    
    if ([self.type isEqualToString:@"two"]) {
        
        self.yuanView.briStr = str;
    }else if ([self.type isEqualToString:@"three"]){
        
        self.xinzuoView.briStr = str;
    }
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
